package edu.nus.mtech.sem1.ca.main;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import edu.nus.mtech.sem1.ca.auth.UserRole;
import edu.nus.mtech.sem1.ca.data.DatabaseLoader;
import edu.nus.mtech.sem1.ca.data.DatabasePersister;
import edu.nus.mtech.sem1.ca.utils.Common;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Main extends JFrame {

	public static JDesktopPane contentPane;
	//JDesktopPane desktop;	

	/**
	 * Create the frame.
	 */
	public Main() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				WriteMemoryToFile();
			}
		});
		setTitle("Telephony");
		contentPane=new JDesktopPane();
		contentPane.setBackground(Color.GRAY);
		setContentPane(contentPane);
		contentPane.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("File");
		mnFile.setMnemonic(KeyEvent.VK_F);
		menuBar.add(mnFile);
		
		
		JMenuItem mntmExiit = new JMenuItem("Save and Exit");
		mntmExiit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				WriteMemoryToFile();
				System.exit(ABORT);
			}
		});
		mntmExiit.setMnemonic(KeyEvent.VK_X);
		mnFile.add(mntmExiit);
		
		JMenu mnAdministration = new JMenu("Administration");
		mnAdministration.setMnemonic(KeyEvent.VK_A);
		menuBar.add(mnAdministration);
		
		JMenu mnCustomer = new JMenu("Customer");
		mnCustomer.setMnemonic(KeyEvent.VK_C);		
		mnAdministration.add(mnCustomer);
		
		JMenuItem mntmRegister = new JMenuItem("Register");
		mntmRegister.setMnemonic(KeyEvent.VK_E);		
		mntmRegister.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub				
				FormCustomer l_customer=new FormCustomer();
				contentPane.add(l_customer);
				Common.OpenCenterScreen(l_customer, true);				
			}
		});
		mnCustomer.add(mntmRegister);
		/*
		JMenuItem mntmView = new JMenuItem("View");
		mntmView.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				FormAllCustomers formAllCustomers=new FormAllCustomers();
				contentPane.add(formAllCustomers);
				Common.OpenCenterScreen(formAllCustomers, true);
			}
		});		
		mntmView.setMnemonic(KeyEvent.VK_V);
		mnCustomer.add(mntmView);
		mntmView.setVisible(false);
		
		mnCustomer.add(mntmView);*/

		JMenuItem mntmSearch = new JMenuItem("Search");
		mntmSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				FormAllCustomers allCustomers=new FormAllCustomers();
				contentPane.add(allCustomers);
				Common.OpenCenterScreen(allCustomers, true);
			}
		});		
		mntmSearch.setMnemonic(KeyEvent.VK_S);
		mnCustomer.add(mntmSearch);
		mntmSearch.setVisible(true);
		
		mnCustomer.add(mntmSearch);
	
		JMenu mnComplaint = new JMenu("Complaint");
		mnComplaint.setMnemonic(KeyEvent.VK_M);
		mnAdministration.add(mnComplaint);
		
		JMenuItem mntmSearchComplaint = new JMenuItem("Search");
		mntmSearchComplaint.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				FormAllComplaints allComplaints=new FormAllComplaints();
				contentPane.add(allComplaints);
				Common.OpenCenterScreen(allComplaints, true);
			}
		});		
		mntmSearchComplaint.setMnemonic(KeyEvent.VK_S);
		//mnCustomer.add(mntmSearchComplaint);
		mntmSearchComplaint.setVisible(true);
		
		mnComplaint.add(mntmSearchComplaint);
		
		
		JMenuItem mntmLogComplaint = new JMenuItem("New Complaint");		
		mntmLogComplaint.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg1) {		
				FormComplaint newComplaint=new FormComplaint();
				contentPane.add(newComplaint);
				Common.OpenCenterScreen(newComplaint, true);
			}
		});
		mntmLogComplaint.setMnemonic(KeyEvent.VK_L);
		mnComplaint.add(mntmLogComplaint);
		
		JMenu mnNewMenu = new JMenu("Bill");
		mnNewMenu.setMnemonic(KeyEvent.VK_B);
		menuBar.add(mnNewMenu);
		
		JMenuItem mntmOverall = new JMenuItem("Generate Bill");
		
		mntmOverall.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				FormBillGenerator billGenerator=new FormBillGenerator();
				contentPane.add(billGenerator);
				Common.OpenCenterScreen(billGenerator, true);
			}
		});
		mntmOverall.setMnemonic(KeyEvent.VK_O);
		mnNewMenu.add(mntmOverall);
		
		JMenu mnHelp = new JMenu("Help");
		mnHelp.setMnemonic(KeyEvent.VK_H);
		menuBar.add(mnHelp);
		
		JMenuItem mntmAbout = new JMenuItem("About");
		mntmAbout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg8) {
				FormAbout about=new FormAbout();
				contentPane.add(about);
				Common.OpenCenterScreen(about, true);
//				frmAbout l_about=new frmAbout();
//				contentPane.add(l_about);
//				Common.OpenCenterScreen(l_about, true);
			}
		});
		mntmAbout.setMnemonic(KeyEvent.VK_A);
		mnHelp.add(mntmAbout);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 751, 506);
		
		
		
	}
	private void WriteMemoryToFile(){
		DatabasePersister persister=new DatabasePersister(DatabaseLoader.getInstance().getDatabase());
		persister.persist();
	}

}
